package com.project.webservice.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.webservice.entity.TUserFunc;
import com.project.webservice.mapper.TUserFuncMapper;
import com.project.webservice.service.TUserFuncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class TUserFuncServiceImpl extends ServiceImpl<TUserFuncMapper, TUserFunc> implements TUserFuncService {

    /**
     *
     * 更新功能权限
     * @param uid
     * @param adds
     * @param dels
     */
    @DS("rfid")
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void  proUpdateUserFuncByUID(String uid,String adds,String dels)throws Exception{
        log.info("proUpdateUserFuncByUID uid:{},adds:{},dels:{}",uid,adds,dels);

//        //删除取消的功能权限
//        super.remove(Wrappers.<TUserFunc>lambdaQuery().eq(TUserFunc::getUid, uid));

        //新增的功能权限
        if(StringUtils.isNotBlank(adds)){
            String[] addsArr = adds.split(",");
            List<TUserFunc> tUserFuncs = new ArrayList<>();
            for(String add:addsArr){
                TUserFunc tUserFunc = new TUserFunc();
                tUserFunc.setUid(uid);
                tUserFunc.setFid(add);
                tUserFuncs.add(tUserFunc);

            }
            super.saveBatch(tUserFuncs);
            //删除取消的功能权限
            if(StringUtils.isNotBlank(dels)) {
                String[] delsArr = dels.split(",");
                super.remove(Wrappers.<TUserFunc>lambdaQuery().eq(TUserFunc::getUid, uid)
                        .in(TUserFunc::getFid, Arrays.asList(delsArr)));
            }

        }
    }
}
